package com.wkhyapp.lm.fragment;

import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;

import com.wkhyapp.lm.R;
import com.wkhyapp.lm.adapter.CCategoryAdapter;
import com.wkhyapp.lm.adapter.CCategoryItemAdapter;
import com.wkhyapp.lm.adapter.CategoryItemAdapter;
import com.wkhyapp.lm.base.SuperFragment;
import com.wkhyapp.lm.contract.CategoryPresenter;
import com.wkhyapp.lm.contract.CategoryView;
import com.wkhyapp.lm.http.vo.CCategory;
import com.wkhyapp.lm.http.vo.Category;
import com.wkhyapp.lm.http.vo.CategoryItem;
import com.wkhyapp.lm.view.SearchActivity;
import com.wkhyapp.lm.view.SearchResultActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class CategoryFragment extends SuperFragment<CategoryPresenter> implements CategoryView {


  //  private CCategoryAdapter cCategoryAdapter;
    private CCategoryItemAdapter cCategoryItemAdapter;
    private CategoryItemAdapter categoryItemAdapter;
    @BindView(R.id.ccategory_lv)
     ListView listView;
    @BindView(R.id.ccitem_lv)
     GridView gridView;
    private List<CCategory> cCategories;
    private List<CategoryItem> categoryItems;
    @BindView(R.id.imageView)
    ImageView imageView;
    @Override
    public void initUI() {
        cCategories = new ArrayList<>();
        categoryItems = new ArrayList<>();
        categoryItemAdapter = new CategoryItemAdapter(context,cCategories);
        listView.setAdapter(categoryItemAdapter);
        categoryItemAdapter.notifyDataSetChanged();

        cCategoryItemAdapter = new CCategoryItemAdapter(context,categoryItems,R.layout.item_ccategoey_item);
        gridView.setAdapter(cCategoryItemAdapter);
        cCategoryItemAdapter.notifyDataSetChanged();

    }

    @Override
    protected void initData() {
        showLoad("加载中...");
        mPresenter.getData();
    }

    @Override
    protected void initListener() {
//        cCategoryAdapter.setCallBack(new CCategoryAdapter.callBack() {
//            @Override
//            public void jump(CCategory item,int index) {
//                cCategoryAdapter.setItem(index);
//                categoryItems.clear();
//                categoryItems.addAll(item.getItemList());
//                cCategoryItemAdapter.notifyDataSetChanged();
//                cCategoryAdapter.notifyDataSetChanged();
//            }
//        });


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                categoryItemAdapter.setSelectItem(position);
                categoryItemAdapter.notifyDataSetInvalidated();
                categoryItems.clear();
                categoryItems.addAll(cCategories.get(position).getItemList());
                cCategoryItemAdapter.notifyDataSetChanged();
            }
        });

        cCategoryItemAdapter.setCallBack(new CCategoryItemAdapter.callBack() {
            @Override
            public void jump(String item,int categryId) {
                goTo(SearchResultActivity.class,item,categryId);
            }
        });

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goTo(SearchActivity.class);
            }
        });
    }

    @Override
    protected CategoryPresenter createPresenter() {
        return mPresenter = new CategoryPresenter(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_category;
    }


    @Override
    public void setData(List<CCategory> list) {
        dismissLoad();
        cCategories.addAll(list);
        categoryItemAdapter.setSelectItem(0);
        categoryItemAdapter.notifyDataSetChanged();
        if(cCategories!=null){
            categoryItems.addAll(cCategories.get(0).getItemList());
            cCategoryItemAdapter.notifyDataSetChanged();
        }
    }
}
